package com.skycong.hello77.activity.sqlitedb;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import com.skycong.hello77.R;

/**
 * @author ruanmingcong
 * @date 2018/1/14 16:24
 */
public class SQLiteDBActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite_db);

        //每个程序都有自己的数据库，默认情况下是各自互相不干扰的
        //创建一个数据库并打开
        SQLiteDatabase db = openOrCreateDatabase("user.db",MODE_PRIVATE,null);
        //执行Sql语句
//        String sql = "CREATE TABLE COMPANY " +
//                "(ID INT PRIMARY KEY     NOT NULL," +
//                " NAME           TEXT    NOT NULL, " +
//                " AGE            INT     NOT NULL, " +
//                " ADDRESS        CHAR(50), " +
//                " SALARY         REAL)";
        db.execSQL("create table if not exists usertb(_id integer primary key autoincrement,name text not null,age int not null,sex text not null)");
        String sql = "insert into usertb(name,sex,age) values('张三','男',20)";
        db.execSQL(sql);
        //查询数据库
        Cursor result = db.rawQuery("select * from usertb", null);
        if(result!=null){
            while (result.moveToNext()){
                Log.i("tag",result.getString(result.getColumnIndex("_id")));
            }
            result.close();//释放Cursor
        }
        db.close();//释放数据库
    }
}
